package cn.alpha.intell.rbtlib.share;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import java.util.List;

import cn.alpha.intell.rbtlib.utils.StringUtil;

/**
 * Created by zhangxiliang on 2016/11/1.
 */
public class ShareContentResolver {

    public static final String METHOD_CONTACT="getContact";
    public static final String METHOD_CLOSE_MONITOR="closeMonitor";
    public static final String METHOD_IM_STATE="getIMState";
    public static final String METHOD_SEND_MSGTO_MOB="METHOD_SEND_MSGTO_MOB";
    public final String  KEY="shareUsers";
    public static final String KEY_IM_STATE="KEY_IM_STATE";


    public  final String schme="content://cn.alpha.intell.robot.telephone.ChatContentProvider";

    public final  Uri uri=Uri.parse(schme);
    public static ShareContentResolver getInstance() {
        return InstanceHolder.instance;
    }

    public List<Contact> getContactFromContentProvider(Context context,ProviderCallBack callBack){

        List<Contact> contactList= null;
        try {
            Bundle bundle=context.getContentResolver().call(uri,METHOD_CONTACT,"",new Bundle());

            Log.e("ShareContentResolver","get Bundle OK");
            contactList = null;
            if(bundle!=null){
                 contactList=(List<Contact>)bundle.getSerializable(KEY);
                if(callBack!=null){
                    callBack.onSuccess(contactList);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return contactList;

    }


    public void closeMonitorIfExist(Context context){
        try {
            context.getContentResolver().call(uri,METHOD_CLOSE_MONITOR,"",new Bundle());
        }catch (Exception e){

        }
    }

    public boolean isConnectedIM(Context context){
        Bundle bundle=null;
        try {
            bundle =context.getContentResolver().call(uri,METHOD_IM_STATE,"",new Bundle());
            if (bundle!=null){
                return  bundle.getBoolean(KEY_IM_STATE);
            }
        }catch (Exception e){

        }


        return false;
    }


    static class InstanceHolder {
        final static ShareContentResolver instance = new ShareContentResolver();
    }



    public interface ProviderCallBack{
        void onSuccess(List<Contact> contactList);
    }

}
